package com.palfund.nested;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;

public class RVMultiTypeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rvmulti_type);
        Toolbar toolbar = findViewById(R.id.toolBar);
        setSupportActionBar(toolbar);
        setTitle("MultiType");
        RecyclerView recyclerViewBottom = findViewById(R.id.recyclerViewBottom);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerViewBottom.setLayoutManager(linearLayoutManager);
        recyclerViewBottom.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int first = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                int last = linearLayoutManager.findLastCompletelyVisibleItemPosition();
                if (first == 0) {
                    Toast.makeText(RVMultiTypeActivity.this, "Top", Toast.LENGTH_SHORT).show();
                }
                if (last == linearLayoutManager.getItemCount() - 1) {
                    Toast.makeText(RVMultiTypeActivity.this, "Bottom", Toast.LENGTH_SHORT).show();
                }
                Log.i("ScrollingActivity", "---onScrolled()--->" + dy);
            }
        });

        ArrayList<ArrayList<String>> rrData = new ArrayList<>();
        for (char i = 'A'; i < 'Z'; i++) {
            ArrayList<String> data = new ArrayList<>();
            for (int j = 0; j < 66; j++) {
                data.add(i + "   " + j);
            }
            rrData.add(data);
        }
        MultiTypeAdapter multiTypeAdapter = new MultiTypeAdapter(this);
        recyclerViewBottom.setAdapter(multiTypeAdapter);
        multiTypeAdapter.addAll(rrData);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "AppBarLayout");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                startActivity(new Intent(this, AppBarRVActivity.class));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
